package classes.pet 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import classes.savedata.RockmanData;
	
	/**
	 * ...
	 * @author acydan
	 */
	public class SkillControl extends MovieClip
	{
		private static const skillName : Array = ["skill_1", "skill_2", "skill_3", "skill_4", "skill_5", "skill_6", "skill_7"];
		private static const skillAddedTxtArr : Array = ["skill_1_added","skill_2_added","skill_3_added","skill_4_added","skill_5_added","skill_6_added","skill_7_added"];
		private static const skillTotalTxtArr : Array = ["skill_1_total","skill_2_total","skill_3_total","skill_4_total","skill_5_total","skill_6_total","skill_7_total"];
		private static const canAddedSkillColor:Number = 0x00FF00;//开锁的技能颜色
		private static const canntAddedSkillColor:Number = 0x000000;//没开锁的技能颜色
		private static const canAdd_2 : int = 10;//能加第二排技能的条件
		private static const canAdd_3 : int = 16;//能加第三排技能的条件
		private static const canAdd_4 : int = 18;//能加第四排技能的条件
		private var addedSkillArr : Array;//存储已加的技能点数
		private var tempAddedSkillArr:Array;//临时存储已加的技能点数
		private var totalSkillArr : Array;//存储每个技能的技能点上限
		private var fileNo:int;//玩家拥有的宠物的序列号
		private var skillsArr:Array;//存储玩家宠物的技能，是一个二维数组
		private var gameData:Object;
		private var copyPet:Bitmap;
		private var skillCanAddNo:int;//剩余的技能点数
		private var petObjectIndex:Array;
		public function SkillControl() 
		{
			
		}
		private function addCurrentPetToStage(_pet : *):void
		{
			trace(_pet.width,_pet.height,"9000090909090");
			var bitPet : BitmapData = new BitmapData(_pet.width,_pet.height,true,0);
			bitPet.draw(_pet);
			copyPet = new Bitmap(bitPet); 
			this.addChildAt(copyPet, numChildren);
			copyPet.x = 500+copyPet.width/2;
			copyPet.y = 150+copyPet.height/2;
		}
		private function showSkillAddedStatus(skillAddedNo : Array,skillTotalNo : Array):void
		{			
			skillRemoveEvent();
			for (var i : int = 0; i < skillTotalNo.length; i++ )
			{
				this[skillTotalTxtArr[i]].text = "/" + skillTotalNo[i].toString();
				this[skillTotalTxtArr[i]].selectable = false;
				this[skillAddedTxtArr[i]].text = skillAddedNo[i].toString();
				this[skillAddedTxtArr[i]].selectable = false;
			}			
			for (var m1 : int = 0; m1 < skillTotalTxtArr.length; m1++ ) 
			{
				this[skillTotalTxtArr[m1]].textColor = canntAddedSkillColor;
				this[skillAddedTxtArr[m1]].textColor = canntAddedSkillColor;
			}
			if (skillAddedNo[0] + skillAddedNo[1] + skillAddedNo[2] < canAdd_2)
			{
				for (var m : int = 0; m < 3; m++ )
				{
					this[skillTotalTxtArr[m]].textColor = canAddedSkillColor;
					this[skillAddedTxtArr[m]].textColor = canAddedSkillColor;
				}
			}
			if (skillAddedNo[0] + skillAddedNo[1] + skillAddedNo[2] >= canAdd_2) {
				
				for (var j : int = 0; j < 5; j++ ) 
				{
					this[skillAddedTxtArr[j]].textColor = canAddedSkillColor;
					this[skillTotalTxtArr[j]].textColor = canAddedSkillColor;
				}
			}
			if (skillAddedNo[0] + skillAddedNo[1] + skillAddedNo[2] + skillAddedNo[3] +skillAddedNo[4] >= canAdd_3) 
			{				
				for (var n : int = 0; n < 6; n++ )
				{
					this[skillAddedTxtArr[n]].textColor = canAddedSkillColor;
					this[skillTotalTxtArr[n]].textColor = canAddedSkillColor;
				}
			}
			if (skillAddedNo[0] + skillAddedNo[1] + skillAddedNo[2] + skillAddedNo[3] +skillAddedNo[4] +skillAddedNo[5] >= canAdd_4)
			{
				for (var l :int = 0; l < skillAddedNo.length; l++ ) 
				{
					this[skillAddedTxtArr[l]].textColor = canAddedSkillColor;
					this[skillTotalTxtArr[l]].textColor = canAddedSkillColor;
				}
			}
			skillAddEvent();
		}
		private function skillRemoveEvent():void
		{
			for (var i1 : int = 0; i1 <skillAddedTxtArr.length; i1++ )
			{
				(getChildByName(skillName[i1]) as MovieClip).removeEventListener(MouseEvent.CLICK, clickHandler);
				(getChildByName(skillName[i1]) as MovieClip).buttonMode = false;
			}
		}
		private function skillAddEvent():void
		{
			for (var i : int = 0; i <skillAddedTxtArr.length; i++ )
			{
				if (this[skillAddedTxtArr[i]].textColor == canAddedSkillColor)
				{
					(getChildByName(skillName[i]) as MovieClip).addEventListener(MouseEvent.CLICK, clickHandler);
					(getChildByName(skillName[i]) as MovieClip).buttonMode = true;
				}
			}
		}
		public function setSkillType(_currentPetName : String,_pet : *,_gameData : Object,_fileNo : int,_petObjectIndex:Array):void
		{
			petObjectIndex = _petObjectIndex;
			gameData = _gameData;			
			fileNo = _fileNo;//宠物序列号
			skillsArr = gameData.pet[petObjectIndex[fileNo]].skills;	
			skillCanAddNo = gameData.pet[petObjectIndex[fileNo]].skillcount;
			remainderNo.text = skillCanAddNo.toString();
			remainderNo.selectable = false;
			var _skillAddedNo:Array = new Array();//已加的技能点数;
			var _skillTotalNo:Array = new Array();//技能总数;
			for (var i:int = 0; i < skillsArr.length; i++ )
			{
				_skillAddedNo.push(skillsArr[i][0]);
				_skillTotalNo.push(skillsArr[i][1]);
			}			
			tempAddedSkillArr = new Array();
			for (var p : int = 0; p < _skillAddedNo.length; p++ ) 
			{
				tempAddedSkillArr.push(_skillAddedNo[p]);
			}
			addedSkillArr = new Array();
			addedSkillArr = _skillAddedNo;
			totalSkillArr = new Array();
			totalSkillArr = _skillTotalNo;
			addCurrentPetToStage(_pet);
			showSkillAddedStatus(_skillAddedNo,_skillTotalNo);
			backBtn.addEventListener(MouseEvent.CLICK, clickHandler);
			sureBtn.addEventListener(MouseEvent.CLICK, clickHandler);
			cancelBtn.addEventListener(MouseEvent.CLICK, clickHandler);
			backBtn.buttonMode = true;
			sureBtn.buttonMode = true;
			cancelBtn.buttonMode = true;
			switch(_currentPetName)
			{
				case "Ice1":
				case "Ice2":
				case "Ice3":
					skillTypeTxt.text = "冰系天赋";					
					break;
				case "Wind1":
				case "Wind2":
				case "Wind3":
					skillTypeTxt.text = "风系天赋";
					break;
				case "Fire1":
				case "Fire2":
				case "Fire3":
					skillTypeTxt.text = "火系天赋";
					break;
				case "Thunder1":
				case "Thunder2":
				case "Thunder3":
					skillTypeTxt.text = "雷系天赋";
					break;
				case "Water1":
				case "Water2":
				case "Water3":
					skillTypeTxt.text = "水系天赋";
					break;
				case "Physics1":
				case "Physics2":
				case "Physics3":
					skillTypeTxt.text = "物理系天赋";
					break;
			}
			for (var j : int = 0; j < skillName.length; j++ )
			{
				(getChildByName(skillName[j]) as MovieClip).gotoAndStop(getSkillType(_currentPetName));				
			}
			skillTypeTxt.selectable = false;
		}
		private function clickHandler(evt : MouseEvent):void
		{
			var clickedMcName : String = evt.target.name;
			switch(clickedMcName)
			{
				case "backBtn":
					backBtn.removeEventListener(MouseEvent.CLICK, clickHandler);
					while(this.numChildren > 0)
					{
						this.removeChildAt(0);
					}
					break;
				case "sureBtn":
					trace("确定加点了，发送数据", tempAddedSkillArr);
					for (var i:int = 0; i < tempAddedSkillArr.length; i++ )
					{
						gameData.pet[petObjectIndex[fileNo]].skills[i][0] = tempAddedSkillArr[i];
					}
					gameData.pet[petObjectIndex[fileNo]].skillcount = skillCanAddNo;
					RockmanData.saveData(gameData);
					break;
				case "cancelBtn":
					trace("cancel added skills", addedSkillArr);
					addedSkillArr = new Array();
					for (var h:int = 0; h < gameData.pet[petObjectIndex[fileNo]].skills.length; h++ )
					{
						addedSkillArr.push(gameData.pet[petObjectIndex[fileNo]].skills[h][0]);
					}					
					showSkillAddedStatus(addedSkillArr, totalSkillArr);
					for (var j : int = 0; j < addedSkillArr.length; j++ ) 
					{
						tempAddedSkillArr[j] = addedSkillArr[j];
					}
					skillCanAddNo = gameData.pet[petObjectIndex[fileNo]].skillcount;
					remainderNo.text = skillCanAddNo.toString();
					remainderNo.selectable = false;
					break;
				default : 
					trace(clickedMcName, "clicked skill", clickedMcName.slice(6));
					var tmpSkillNo : int = int(clickedMcName.slice(6));
					disposalSkill(tmpSkillNo);
			}		
		}
		private function disposalSkill(tmpSkillNo : int):void
		{
			trace(addedSkillArr, "added Skills");
			if (tempAddedSkillArr[tmpSkillNo - 1] < totalSkillArr[tmpSkillNo - 1]&&skillCanAddNo > 0)
			{
				tempAddedSkillArr[tmpSkillNo - 1]++;
				skillCanAddNo--;
				remainderNo.text = skillCanAddNo.toString();
				showSkillAddedStatus(tempAddedSkillArr,totalSkillArr);
			}
		}
		private function getSkillType(_tempName:String):String
		{
			var _tempArr:String;
			for (var i:int = 0; i < _tempName.length; i++ )
			{
				if(_tempName.charCodeAt(i)>48 && _tempName.charCodeAt(i) < 58)
				{
					trace(_tempName.substr(0, i), "oooooo");
					_tempArr = _tempName.substr(0, i);
					break;
				}
			}
			return _tempArr;
		}
	}
}